<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>类型声明</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.types.iterable.html">« Iterable 可迭代对象</a></li>
      <li style="float: right;"><a href="language.types.type-juggling.html">类型转换 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.types.html">类型</a></li>
    <li>类型声明</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.types.declarations" class="sect1">
 <h2 class="title">类型声明</h2>

 <p class="para">
  类型声明可以用于函数的参数、返回值，PHP 7.4.0
  起还可以用于类的属性，来显性的指定需要的类型，如果预期类型在调用时不匹配，则会抛出一个
  <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span> 异常。
 </p>

 
 <blockquote class="note"><p><strong class="note">注意</strong>: 
  
  <p class="para">
   当子类覆盖父方法时，子类的方法必须匹配父类的类型声明。如果父类没有定义返回类型，那么子方法可以指定自己的返回类型。
  </p>
 </p></blockquote>

 <div class="sect2" id="language.types.declarations.base">
  <h3 class="title">单一类型</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>类型</th>
      <th>说明</th>
      <th>版本</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>类/接口 名称</td>
      <td>
        值必须为指定类和接口的实例化对象 <a href="language.operators.type.html" class="link"><code class="literal">instanceof</code></a> 
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type"><span class="type self">self</span></span></td>
      <td>
      值必须是用于类型声明相同类的 <a href="language.operators.type.html" class="link"><code class="literal">instanceof</code></a> 。
       仅可在类中使用。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type"><span class="type parent">parent</span></span></td>
      <td>
       值必须是用于类型声明父级类的 <a href="language.operators.type.html" class="link"><code class="literal">instanceof</code></a> ，
       仅可在类中使用。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type">array</span></td>
      <td>
        值必须为 <span class="type">array</span>。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span></td>
      <td>
       值必定是一个有效的 <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>。
       不能用于类属性的类型声明。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type">bool</span></td>
      <td>
        值必须为一个布尔值。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type">float</span></td>
      <td>
        值必须为一个浮点数字。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type">int</span></td>
      <td>
        值必须为一个整型数字。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type">string</span></td>
      <td>
        值必须为一个 <span class="type">string</span>。
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><span class="type"><a href="language.types.iterable.html" class="type iterable">iterable</a></span></td>
      <td>
        值必须为 <span class="type">array</span> 或 <a href="language.operators.type.html" class="link"><code class="literal">instanceof</code></a> <span class="classname"><a href="class.traversable.html" class="classname">Traversable</a></span>。
      </td>
      <td>PHP 7.1.0</td>
     </tr>

     <tr>
      <td><span class="type">object</span></td>
      <td>
        值必须为<span class="type">object</span>。
      </td>
      <td>PHP 7.2.0</td>
     </tr>

     <tr>
      <td><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></td>
      <td>
        值可以为任何类型。
      </td>
      <td>PHP 8.0.0</td>
     </tr>

    </tbody>
   
  </table>


  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    不支持上述标量类型的别名。相反，它们被视为类或接口名。例如，使用
    <code class="literal">boolean</code> 作为类型声明，将要求值是一个 <a href="language.operators.type.html" class="link"><code class="literal">instanceof</code></a> 类或接口
    <code class="literal">boolean</code>，而不能是类型
    <span class="type">bool</span>。
   </p>
   <p class="para">
    <div class="example" id="example-74">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">boolean&nbsp;$param</span><span style="color: #007700">)&nbsp;{}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
Warning: &quot;boolean&quot; will be interpreted as a class name. Did you mean &quot;bool&quot;? Write &quot;\boolean&quot; to suppress this warning in /in/9YrUX on line 2

Fatal error: Uncaught TypeError: test(): Argument #1 ($param) must be of type boolean, bool given, called in - on line 3 and defined in -:2
Stack trace:
#0 -(3): test(true)
#1 {main}
  thrown in - on line 2
</pre></div>
     </div>
    </div>
   </p>
  </div>

  <div class="sect3" id="language.types.declarations.mixed">
  <h4 class="title">mixed</h4>

  <p class="para">
   <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> 等同于 <a href="language.types.declarations.html#language.types.declarations.composite.union" class="link">联合类型</a>
   <span class="type"><span class="type">object</span>|<span class="type">resource</span>|<span class="type">array</span>|<span class="type">string</span>|<span class="type">int</span>|<span class="type">float</span>|<span class="type">bool</span>|<span class="type">null</span></span>。PHP 8.0.0 起可用。
  </p>
 </div>

  <div class="sect3" id="language.types.declarations.examples">
   <h4 class="title">范例</h4>
   <div class="example" id="example-75">
    <p><strong>示例 #1 在类中使用类型声明</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br />class&nbsp;</span><span style="color: #0000BB">D&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">//&nbsp;它没有&nbsp;extend&nbsp;C。<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">E&nbsp;</span><span style="color: #007700">{}<br /><br />function&nbsp;</span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">C&nbsp;$c</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">D</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">E</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
C
D

Fatal error: Uncaught TypeError: f(): Argument #1 ($c) must be of type C, E given, called in /in/gLonb on line 14 and defined in /in/gLonb:8
Stack trace:
#0 -(14): f(Object(E))
#1 {main}
  thrown in - on line 8
</pre></div>
    </div>
   </div>

   <div class="example" id="example-76">
    <p><strong>示例 #2 在接口中使用类型声明</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">interface&nbsp;</span><span style="color: #0000BB">I&nbsp;</span><span style="color: #007700">{&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">f</span><span style="color: #007700">();&nbsp;}<br />class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">I&nbsp;</span><span style="color: #007700">{&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">f</span><span style="color: #007700">()&nbsp;{}&nbsp;}<br /><br /></span><span style="color: #FF8000">//&nbsp;它没有&nbsp;implement&nbsp;I。<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">E&nbsp;</span><span style="color: #007700">{}<br /><br />function&nbsp;</span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">I&nbsp;$i</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">E</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
C

Fatal error: Uncaught TypeError: f(): Argument #1 ($i) must be of type I, E given, called in - on line 13 and defined in -:8
Stack trace:
#0 -(13): f(Object(E))
#1 {main}
  thrown in - on line 8
</pre></div>
    </div>
   </div>

   <div class="example" id="example-77">
    <p><strong>示例 #3 返回类型声明</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">float&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;注意必须返回一个&nbsp;float。<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
float(3)
</pre></div>
    </div>
   </div>

   <div class="example" id="example-78">
    <p><strong>示例 #4 返回一个对象</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br /><br />function&nbsp;</span><span style="color: #0000BB">getC</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">getC</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(C)#1 (0) {
}
</pre></div>
    </div>
   </div>
  </div>
 </div>

 <div class="sect2" id="language.types.declarations.nullable">
  <h3 class="title">允许为 null 的类型</h3>

  <p class="para">
    自 PHP 7.1.0 起，类型声明允许前置一个问号 (<code class="literal">?</code>)
    用来声明这个值允许为指定类型，或者为 <strong><code>null</code></strong>。
  </p>

  <p class="para">
   <div class="example" id="example-79">
    <p><strong>示例 #5 声明可为 null 的参数类型</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br /><br />function&nbsp;</span><span style="color: #0000BB">f</span><span style="color: #007700">(?</span><span style="color: #0000BB">C&nbsp;$c</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(C)#1 (0) {
}
NULL
</pre></div>
    </div>
   </div>

   <div class="example" id="example-80">
    <p><strong>示例 #6 声明可为 null 的返回类型</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">get_item</span><span style="color: #007700">():&nbsp;?</span><span style="color: #0000BB">string&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'item'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'item'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    可以通过设置参数的默认值为 <code class="literal">null</code>
    来实现允许为空的参数。不建议这样做，因为影响到类的继承调用。
   </p>
   <div class="example" id="example-81">
    <p><strong>示例 #7 旧版本中实现允许为空参数的示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br /><br />function&nbsp;</span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">C&nbsp;$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">f</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(C)#1 (0) {
}
NULL
</pre></div>
    </div>
   </div>
  </p></blockquote>
 </div>

 <div class="sect2" id="language.types.declarations.composite">
  <h3 class="title">复合类型</h3>
  <p class="para">
   还可以组合简单类型为复合类型。
   PHP 支持以下方式复合类型：
  </p>

  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     简单类型联合。PHP 8.0.0 起可用。
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     class-type（接口、类名）类型交集。PHP 8.1.0 起可用。
    </span>
   </li>
  </ul>

  <div class="caution"><strong class="caution">警告</strong>
   <p class="simpara">
    无法复合交集类型与联合类型。
   </p>
  </div>

  <div class="sect3" id="language.types.declarations.composite.union">
   <h4 class="title">联合类型</h4>
   <p class="para">
     联合类型接受多个不同的简单类型做为参数。声明联合类型的语法为
     <code class="literal">T1|T2|...</code>。联合类型自 PHP 8.0.0 起可用。
   </p>

   <div class="sect4" id="language.types.declarations.composite.union.nullable">
    <h5 class="title">允许为空的联合类型</h5>
    <p class="para">
     <code class="literal">null</code> 类型允许在联合类型中使用，例如
     <code class="literal">T1|T2|null</code> 代表接受一个空值为参数。已经存在的 <code class="literal">?T</code>
     语法可以视为以下联合类型的一个简写 <code class="literal">T|null</code>。
    </p>

    <div class="caution"><strong class="caution">警告</strong>
     <p class="simpara">
      <code class="literal">null</code> 不能作为一个独立的类型使用。
     </p>
    </div>
   </div>

   <div class="sect4" id="language.types.declarations.composite.union.false">
    <h5 class="title">false 伪类型</h5>
    <p class="para">
     通过联合类型支持字面类型（Literal Type）<code class="literal">false</code>，
     出于历史原因，很多内部函数在失败时返回了 <code class="literal">false</code> 而不是 <code class="literal">null</code>。
     这类函数的典型例子是 <span class="function"><a href="function.strpos.html" class="function">strpos()</a></span>。
    </p>

    <div class="caution"><strong class="caution">警告</strong>
     <p class="simpara">
      <code class="literal">false</code> 不能单独作为类型使用（包括可为 null 独立类型）。因此，不可以用
      <code class="literal">false</code>、<code class="literal">false|null</code>、<code class="literal">?false</code>。
     </p>
    </div>
    <div class="caution"><strong class="caution">警告</strong>
     <p class="simpara">
      <code class="literal">true</code> 字面类型<em class="emphasis">不存在</em>。
     </p>
    </div>
   </div>
  </div>

  <div class="sect3" id="language.types.declarations.composite.intersection">
   <h4 class="title">交集类型</h4>
   <p class="para">
    只要能满足 class-type 的值，都可以在交集类型声明中使用，并且可使用多个值。
    交集类型用 <code class="literal">T1&amp;T2&amp;...</code> 这样的语法指定。
    交集类型从 PHP 8.1.0 可以使用。
   </p>
  </div>

  <div class="sect3" id="language.types.declarations.composite.redundant">
   <h4 class="title">重复冗余的类型</h4>
   <p class="para">
    为了能捕获复合类型声明中的简单错误，不需要类加载检测到的冗余类型将导致编译时错误。包含：

    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       解析出来的类型只能出现一次。例如这样的类型 <code class="literal">int|string|INT</code> 或
       <code class="literal">Countable&amp;Traversable&amp;COUNTABLE</code> 会导致错误。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       使用 <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> 会导致错误。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">对于联合类型：</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         使用了 <span class="type">bool</span> 时就不能再附带使用 <span class="type"><span class="type false">false</span></span>。
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         使用了 <span class="type">object</span> 时就不能再附带使用 class 类型。
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         使用了 <span class="type"><a href="language.types.iterable.html" class="type iterable">iterable</a></span> 时，不能再附带使用 <span class="type">array</span>
         和 <span class="classname"><a href="class.traversable.html" class="classname">Traversable</a></span>。
        </span>
       </li>
      </ul>
     </li>
     <li class="listitem">
      <span class="simpara">对于交集类型：</span>
      <ul class="itemizedlist">
       <li class="listitem">
        <span class="simpara">
         使用 class-type 以外的类型会导致错误。
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         使用 <span class="type"><span class="type self">self</span></span>、<span class="type"><span class="type parent">parent</span></span>
         或 <span class="type"><span class="type static">static</span></span> 都会导致错误。
        </span>
       </li>
      </ul>
     </li>
    </ul>
   </p>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     不过它不能确保类型最小化，因为要达到这样的效果，还要加载使用类型的 class。
    </span>
   </p></blockquote>

   <p class="para">
    例如，假设 <code class="literal">A</code> 和 <code class="literal">B</code> 都是一个类的别名，
    而 <code class="literal">A|B</code> 仍然是有效的，哪怕它可以被简化为
    <code class="literal">A</code> 或
    <code class="literal">B</code>。
    同样的，如果 <code class="code">B extends A {}</code>，那 <code class="literal">A|B</code>
    仍然是有效的联合类型，尽管它可以被简化为
    <code class="literal">A</code>。

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">int</span><span style="color: #007700">|</span><span style="color: #0000BB">INT&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;不允许<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">bool</span><span style="color: #007700">|</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;不允许<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">int</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Traversable&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;不允许<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Traversable&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;不允许<br /><br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">;<br />function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">|</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;不允许&nbsp;("use"&nbsp;是名称解析的一部分)<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;不允许&nbsp;("use"&nbsp;是名称解析的一部分)<br /><br /></span><span style="color: #0000BB">class_alias</span><span style="color: #007700">(</span><span style="color: #DD0000">'X'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Y'</span><span style="color: #007700">);<br />function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">X</span><span style="color: #007700">|</span><span style="color: #0000BB">Y&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;允许&nbsp;(运行时才能知道重复性)<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">X</span><span style="color: #007700">&amp;</span><span style="color: #0000BB">Y&nbsp;</span><span style="color: #007700">{}&nbsp;</span><span style="color: #FF8000">//&nbsp;允许&nbsp;(运行时才能知道重复性)<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>

 </div>

 <div class="sect2" id="language.types.declarations.return-only">
  <h3 class="title">仅仅返回类型</h3>

  <div class="sect3" id="language.types.declarations.void">
   <h4 class="title">void</h4>
   <p class="para">
    <code class="literal">void</code> 是一个返回类型，用于标识函数没有返回值。
    它不能是联合类型的一部分。
    PHP 7.1.0 起可用。
   </p>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     从 PHP 8.1.0 起弃用引用返回 <span class="type"><span class="type void">void</span></span> 的函数，
     因为这样的函数是矛盾的。
     之前，它在调用时已经发出如下 <strong><code>E_NOTICE</code></strong>：
     <code class="literal">Only variable references should be returned by reference</code>。

     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;&amp;</span><span style="color: #0000BB">test</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">void&nbsp;</span><span style="color: #007700">{}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
   </p></blockquote>
  </div>

  <div class="sect3" id="language.types.declarations.never">
   <h4 class="title">never</h4>
   <p class="para">
    <code class="literal">never</code> 是一种表示没有返回的返回类型。这意味着它可能是调用 <span class="function"><a href="function.exit.html" class="function">exit()</a></span>，
    抛出异常或者是一个无限循环。所以，它不能作为联合类型的一部分。PHP 8.1.0 起可用。
   </p>
   <p class="para">
    在类型理论用于中，<span class="type"><span class="type never">never</span></span> 是底层类型。
    意味着它是其它所有类型的子类型，并且可以在继承期间替换任何其他返回类型。
   </p>
  </div>

  <div class="sect3" id="language.types.declarations.static">
   <h4 class="title">static</h4>
   <p class="para">
    它的值必须是一个 class 的 <a href="language.operators.type.html" class="link"><code class="literal">instanceof</code></a>，该 class 是调用方法所在的同一个类。
    PHP 8.0.0 起有效。
   </p>
  </div>
 </div>

 <div class="sect2" id="language.types.declarations.strict">
  <h3 class="title">严格类型</h3>

  <p class="para">
   默认如果可能，PHP 会强制转化不合适的类型为想要的标量类型。
   比如，参数想要 <span class="type">string</span>，传入的是 <span class="type">int</span>，
   则会获取 <span class="type">string</span> 类型的变量。
  </p>

  <p class="para">
   可以按文件开启严格模式。
   在严格模式下，只能接受完全匹配的类型，否则会抛出 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span>。
   唯一的例外是 <span class="type">int</span> 值也可以传入声明为 <span class="type">float</span> 的类型。
  </p>

  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    通过内部函数调用函数时，不会受 <code class="literal">strict_types</code> 声明影响。
   </p>
  </div>

  <p class="para">
   要开启严格模式，使用 <a href="control-structures.declare.html" class="link"><code class="literal">declare</code></a> 开启
   <code class="literal">strict_types</code>：
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    文件开启严格类型后的<em class="emphasis">内部</em>调用函数将应用严格类型，
    而不是在声明函数的文件内开启。
    如果文件没有声明开启严格类型，而被调用的函数所在文件有严格类型声明，
    那将遵循调用者的设置（开启类型强制转化），
    值也会强制转化。
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    只有为标量类型的声明开启严格类型。
   </p>
  </p></blockquote>

  <div class="example" id="example-82">
   <p><strong>示例 #8 参数值的严格类型</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">strict_types</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">int&nbsp;$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(3)

Fatal error: Uncaught TypeError: sum(): Argument #1 ($a) must be of type int, float given, called in - on line 9 and defined in -:4
Stack trace:
#0 -(9): sum(1.5, 2.5)
#1 {main}
  thrown in - on line 4
</pre></div>
   </div>
  </div>

  <div class="example" id="example-83">
   <p><strong>示例 #9 参数值的类型强制转化</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">int&nbsp;$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;以下会强制转化为整型，注意以下内容输出！<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(3)
int(3)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-84">
   <p><strong>示例 #10 返回值的严格类型</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">strict_types</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">int&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(3)

Fatal error: Uncaught TypeError: sum(): Return value must be of type int, float returned in -:5
Stack trace:
#0 -(9): sum(1, 2.5)
#1 {main}
  thrown in - on line 5
</pre></div>
   </div>
  </div>
 </div>

 <div class="sect2" id="language.types.declarations.union.coercive">
  <h3 class="title">联合类型的内部隐式强制转化</h3>
  <p class="para">
   没有开启 <code class="literal">strict_types</code> 时，标量类型可能会限制内部隐式类型转化。
   如果值的类型不是联合类型中的一部分，则目标类型会按以下顺序：

   <ol type="1">
    <li class="listitem">
     <span class="simpara">
      <span class="type">int</span>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <span class="type">float</span>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <span class="type">string</span>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <span class="type">bool</span>
     </span>
    </li>
   </ol>
   如果类型出现在组合中，值可以按 PHP 现有的类型语义检测进行内部隐式强制转化，则会选择该类型。
   否则会尝试下一个类型。
  </p>

  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    有一个例外：当值是字符串，而 int 与 float 同时在组合中，将按现有的“数字字符串”检测语义，识别首选的类型。
    例如，<code class="literal">&quot;42&quot;</code> 会选择 <span class="type">int</span> 类型，
    而 <code class="literal">&quot;42.0&quot;</code> 会选择 <span class="type">float</span> 类型。
   </p>
  </div>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    没有出现在上面列表中的类型则不是有效的内部隐式转化目标。
    尤其是不会出现内部隐式转化 <code class="literal">null</code> 和 <code class="literal">false</code> 类型。
   </p>
  </p></blockquote>

  <div class="example" id="example-85">
   <p><strong>示例 #11 类型强制转换为联合类型的例子</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;int|string<br /></span><span style="color: #0000BB">42&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;类型完全匹配<br /></span><span style="color: #DD0000">"42"&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #DD0000">"42"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;类型完全匹配<br /></span><span style="color: #007700">new&nbsp;</span><span style="color: #0000BB">ObjectWithToString&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #DD0000">"__toString()&nbsp;的结果"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;object&nbsp;不兼容&nbsp;int，降级到&nbsp;string<br /></span><span style="color: #0000BB">42.0&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float&nbsp;与&nbsp;int&nbsp;兼容<br /></span><span style="color: #0000BB">42.1&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float&nbsp;与&nbsp;int&nbsp;兼容<br /></span><span style="color: #0000BB">1e100&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #DD0000">"1.0E+100"&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float&nbsp;比&nbsp;int&nbsp;大太多了，降级到&nbsp;string<br /></span><span style="color: #0000BB">INF&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #DD0000">"INF"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float&nbsp;比&nbsp;int&nbsp;大太多了，降级到&nbsp;string<br /></span><span style="color: #0000BB">true&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool&nbsp;与&nbsp;int&nbsp;兼容<br /></span><span style="color: #007700">[]&nbsp;&nbsp;&nbsp;&nbsp;--&gt;&nbsp;</span><span style="color: #0000BB">TypeError&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;array&nbsp;不兼容&nbsp;int&nbsp;或&nbsp;string<br /><br />//&nbsp;int|float|bool<br /></span><span style="color: #DD0000">"45"&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;int&nbsp;的数字字符串<br /></span><span style="color: #DD0000">"45.0"&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">45.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float&nbsp;的数字字符串<br /><br /></span><span style="color: #DD0000">"45X"&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">true&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;不是数字字符串，降级到&nbsp;bool<br /></span><span style="color: #DD0000">""&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">false&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;不是数字字符串，降级到&nbsp;bool<br /></span><span style="color: #DD0000">"X"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">--&gt;&nbsp;</span><span style="color: #0000BB">true&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;不是数字字符串，降级到&nbsp;bool<br /></span><span style="color: #007700">[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&gt;&nbsp;</span><span style="color: #0000BB">TypeError&nbsp;</span><span style="color: #FF8000">//&nbsp;array&nbsp;不兼容&nbsp;int、float、bool<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="sect2" id="language.types.declarations.misc">
  <h3 class="title">其他</h3>
   <div class="example" id="example-86">
    <p><strong>示例 #12 传引用参数的类型</strong></p>
    <div class="example-contents"><p>
     仅仅会在函数入口检查传引用的参数类型，而不是在函数返回时检查。
     所以函数返回时，参数类型可能会发生变化。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(array&nbsp;&amp;</span><span style="color: #0000BB">$param</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$param&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$var&nbsp;</span><span style="color: #007700">=&nbsp;[];<br /></span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_baz</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int(1)

Fatal error: Uncaught TypeError: array_baz(): Argument #1 ($param) must be of type array, int given, called in - on line 9 and defined in -:2
Stack trace:
#0 -(9): array_baz(1)
#1 {main}
  thrown in - on line 2
</pre></div>
    </div>
   </div>

   <div class="example" id="example-87">
    <p><strong>示例 #13 捕获 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">strict_types</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">int&nbsp;$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2.5</span><span style="color: #007700">));<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">TypeError&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Error:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int(3)
Error: sum(): Argument #1 ($a) must be of type int, float given, called in - on line 10
</pre></div>
    </div>
   </div>
 </div>
</div></div></div></body></html>